package cn.nb12.xbox.utils;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * 异常信息处理
 * @author yh
 *
 */
@Component
public class ExceptionInfo {
	private Logger log=LoggerFactory.getLogger(getClass());
	
	/**
	 * 获取国际化信息
	 * @param request
	 * @param messageSource
	 * @param code
	 * @return
	 */
	public String getMessage(HttpServletRequest request,MessageSource messageSource,String code) {
		try {
			Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
			String message = messageSource.getMessage(code, null, locale);
			if(message==null || message.trim().length()<1) {
				throw new Exception(code);//找不着错误
			}
			return message;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return String.format("NOT FIND ERROR:%s", e.getMessage());
		}
	}
}
